package cool.lichuan.zabbix.request;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author LiChuan
 * @date 2021/11/4 9:30
 */
public class Requester {
    private static final AtomicInteger NEXT_ID = new AtomicInteger(1);

    private final Request request = new Request();


    /** ------------------------ 常规请求构造 ------------------------ */
    public static Request getRequester(String method){
        return new Request(method, NEXT_ID.getAndIncrement());
    }

    public static Request getRequester(String method, HashMap<String, Object> params){
        if (null == params) {
            params = new HashMap<>(4);
        }
        return new Request(method, params, NEXT_ID.getAndIncrement());
    }

    /** ------------------------ 使用builder构造request参数 ------------------------ */
    public static Requester builder() {
        return new Requester();
    }

    public Request build() {
        if (null == request.getId()) {
            request.setId(NEXT_ID.getAndIncrement());
        }
        return request;
    }

    public Requester method(String methodName) {
        request.setMethod(methodName);
        return this;
    }

    public Requester param(String key, String value) {
        request.putParam(key, value);
        return this;
    }

    public Requester auth(String auth) {
        request.setAuth(auth);
        return this;
    }

    public Requester jsonRpc(String jsonRpc) {
        request.setJsonrpc(jsonRpc);
        return this;
    }

    private Requester() {
    }
}
